import time
from datetime import datetime

from api.login_api import loginApi
from utils.RequestUtils import Request
from utils.log import LoggerUtils


class serviceOrderApi:

    def __init__(self):
        self.log = LoggerUtils().logger()
        self.request = Request()

    def store(self):
        """
        获取商店信息接口
        :return:
        """
        url = "https://stage.geer2.com/ym/goodsStore/order/storePage"
        params = {
            "current": 1,
            "size": 20,
            "managementType": 0
        }
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, params=params, headers=headers)
        return response

    def userID(self, storeId=None):
        """
        获取所有用户的信息
        :param storeId:
        :return:
        """
        url = f'https://stage.geer2.com/ym/member/order/customerPage?current=1&size=20&storeId={storeId}&keyWord='
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def storeGoods(self):
        """
        获取商店内部的商品信息
        :return: 返回响应结果
        """

        storeId = serviceOrderApi().store().json()['data']["records"][0].get("storeId")
        customerId = serviceOrderApi().userID(storeId).json()['data']['records'][0].get("id")
        url = f"https://stage.geer2.com/ym/storeGoods/page?pageNo=1&pageSize=20&storeId={storeId}&customerId={customerId}"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_create(self, storeId, userId, Price, goodsId):
        """
        创建服务订单接口
        :param storeId: 商店编号
        :param userId: 用户编号
        :param Price: 价格
        :param goodsId: 商品编号
        :return: 返回响应结果
        """
        url = "https://stage.geer2.com/ym/order/offline/order/create"
        create_time = str(datetime.now())
        data = {
            "createType": "service",
            "isExamineAgree": False,
            "rollbackFlag": 1,
            "counselor": "",
            "revenueFlag": 1,
            "couponCode": "",
            "serviceOrderSaveCmd": {
                "storeId": storeId,
                "userId": userId,
                "couponCode": "",
                "customPayPrice": Price,
                "sellers": [],
                "counselor": "",
                "payMode":
                    "batefly",
                "certificate": [],
                "createTime": create_time.split('.')[0],
                "remark": "",
                "isOpenRevenueStatistics": 1,
                "isSupplementAmount": 1,
                "supplementAmount": "",
                "isFastVerify": 0,
                "techIdList": [],
                "serviceList": [{"num": 1, "serviceId": goodsId, "storeId": storeId}]}}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url, json=data, headers=headers)
        return response

    def all_order_search(self, orderCode=None, pageSize=10, pageNum=1, companyId=319, companyIdName="柒美财润店"):
        """
        条件查询全部订单
        :param orderCode: 订单搜索
        :param pageSize: 页数长度：默认最短10，20,30,40,50,100
        :param pageNum:默认页数从第一页开始，1
        :param companyId: 门店ID
        :param companyIdName: 门店名称
        :return: 返回响应结果
        """
        url = "https://stage.geer2.com/ym/order/page"
        params = {
            "orderType": "all",
            "pageNum": pageNum,
            "pageSize": pageSize,
            "isRefundIgnore": 1,
            "companyId": companyId,
            "orderCode": orderCode,
            "$companyId": companyIdName
        }
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, params=params, headers=headers)
        return response

    def order_editor(self, id):
        """
        编辑服务订单
        :param id:
        :return:
        """
        url = f'https://stage.geer2.com/ym/order/orderEditView/{id}'
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_update(self, orderId, storeId, userId, Goodsid, price):
        """
        确认编辑服务订单接口
        :param orderId:  订单编号
        :param storeId: 商店编号
        :param userId: 用户编号
        :param Goodsid: 服务编号
        :param price: 服务价格
        :return:
        """
        url = "https://stage.geer2.com/ym//order/offline/order/edit"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        create_time = str(datetime.now())
        data = {"createType": "service", "isExamineAgree": False, "orderId": orderId, "rollbackFlag": 1,
                "counselor": "",
                "revenueFlag": 1, "couponCode": "",
                "serviceOrderSaveCmd": {"storeId": storeId, "userId": userId, "couponCode": "", "customPayPrice": price,
                                        "sellers": [], "counselor": "", "payMode": "batefly", "certificate": [],
                                        "createTime": create_time.split('.')[0], "remark": "",
                                        "isOpenRevenueStatistics": 1,
                                        "isSupplementAmount": 1, "supplementAmount": "", "walletPayAmount": 0,
                                        "originCustomerId": userId, "couponName": None, "isFastVerify": 0,
                                        "techIdList": [],
                                        "serviceList": [{"num": 1, "serviceId": Goodsid, "storeId": storeId}]}}
        response = self.request.post(url=url, json=data, headers=headers)
        return response

    def order_details(self, id):
        """
        订单详情接口
        :param id: 订单ID
        :return: 返回响应结果
        """
        url = f"https://stage.geer2.com/ym/order/{id}"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_pass(self, id):
        """
        服务订单审核通过
        :param id:
        :return:
        """
        url = f"https://stage.geer2.com/ym/order/examineAgree/{id}"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_applyRefund(self,text,orderId):
        """
        提出申请退款接口
        :param text:
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/applyRefund"
        data = {"customerRemark": text, "orderId": orderId,
                "refundStatus": {"orderId": orderId, "refundStatus": "applying"}}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url,json=data, headers=headers)
        return response

    def order_agree(self,orderId):
        """
        同意申请退款接口
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/verifyOrderDelete/{}".format(orderId)

        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url,  headers=headers)
        return response

    def order_refund(self,text,orderid):
        """
        拒绝退款
        :param orderid:
        :param text:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/refund"
        data = {"refundReason":text,"orderId":orderid,"refundStatus":"applying"}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url, headers=headers)
        return response

    def order_orderDelete(self,orderId):
        """
        删除服务订单
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/orderDelete/{}".format(orderId)

        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response


    def order_examineRefuse(self,text:str,orderId):
        """
        驳回服务订单
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/examineRefuse"
        data = {"orderId":orderId,"refuseReason":text}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url,json=data, headers=headers)
        return response





if __name__ == '__main__':
    pass
    # print(serviceOrderApi().store().json()['data']['records'][0].get("storeId"))
    # serviceOrderApi().userID(319)
    # serviceOrderApi().storeGoods()
    # storeId = serviceOrderApi().store().json()['data']["records"][0].get("storeId")
    # userId = serviceOrderApi().userID(storeId).json()['data']['records'][0].get("id")
    # GoodsId = serviceOrderApi().storeGoods().json()['data']['records'][0].get("goodsId")
    # price = serviceOrderApi().storeGoods().json()['data']['records'][0].get("price")
    # serviceOrderApi().order_create(storeId=storeId, userId=userId, Price=price, goodsId=GoodsId)
